#ifndef __KT_OTA_H_
#define __KT_OTA_H_

#include <stdint.h>
#include <stdio.h>
#include "app_delay.h"

#include "easyjs.h"



#define OTAMSGERR_MODEL     0xFFFFFFFF
#define OTAMSGERR_VERSION   0xFFFFFFFE
#define OTAMSGERR_OTHER     0xFFFFFFF0

#define KTOTA_MAX_LOAD_LEN 1024 /* 文件信息载荷 */
typedef struct 
{
	uint32_t model;
	uint32_t version;
	uint32_t buildtime;
	uint32_t crc32val;
	uint32_t file_len;
	uint32_t data_len;
	uint32_t region_idx;
	uint32_t flash_bak_base;
	uint32_t flash_data[KTOTA_MAX_LOAD_LEN/(sizeof(uint32_t))];
}KTOTA_REGION;
#define KTOTA_MAX_PACK_LEN  (sizeof(KTOTA_REGION))
#define KTOTA_HEAD_LEN      ((sizeof(uint32_t)*8))

typedef struct 
{
	uint32_t model;
	uint32_t version;
	uint32_t buildtime;
	uint32_t crc32val;
	uint32_t head_len;
	uint32_t data_len;
	uint32_t reserved1;
	uint32_t reserved2;
	uint32_t align[120];
}bin_head;
#define BIN_HEAD_LEN (sizeof(bin_head))

/* 需要的外部接口 */
uint32_t ota_calcrc32 (const uint8_t *data, uint32_t size);
/* ------------- */

/* 从机需要的接口 */
extern int32_t ota_flash_erase(void); /* 擦写文件存储区 */
extern void ota_flash_write(uint32_t addr, uint32_t *data, uint32_t len); /* 写FLASH内容 */
extern void ota_msg_send(uint8_t *buf, uint32_t len); /* 通信写 */
/* -------------- */


uint8_t verify_app_head(const uint8_t *binbuf);
uint8_t verify_app_data(const uint8_t *binbuf);


#endif




